*&---------------------------------------------------------------------*
*& 包含               ZPPR128_M01
*&---------------------------------------------------------------------*

CLASS zcl_excel IMPLEMENTATION .

  METHOD read_excel .

    DATA: lv_filename TYPE rlgrap-filename .

    IF p_file IS INITIAL.
      MESSAGE s000(oo) WITH '请输入文件目录' DISPLAY LIKE 'E' .
      RETURN .
    ENDIF.

    lv_filename = p_file .

    CALL FUNCTION 'ZALSM_EXCEL_TO_INTERNAL_TABLE'
      EXPORTING
        filename                = lv_filename
        i_begin_col             = 1
        i_begin_row             = 2
        i_end_col               = 99
        i_end_row               = 10001 "MAX 28200
*       IV_SHEET_NAME           =
      TABLES
        intern                  = gt_excel
      EXCEPTIONS
        inconsistent_parameters = 1
        upload_ole              = 2
        OTHERS                  = 3.

    IF gt_excel[] IS INITIAL.
      RETURN.
    ENDIF.

    LOOP AT gt_excel INTO DATA(ls_excel).

      CASE ls_excel-col.
        WHEN 1. " 序号
          gs_data-num = ls_excel-value.
        WHEN 2. " 原物料编码
          gs_data-matnr_orgin = ls_excel-value .
          gs_data-matnr_orgin = |{ gs_data-matnr_orgin ALPHA = IN WIDTH = 18 }| .
        WHEN 3. " 原序列号
          gs_data-sernr_orgin = ls_excel-value .
        WHEN 4. " 原批次号
          gs_data-charg_orgin = ls_excel-value .
        WHEN 5. " 新物料编码
          gs_data-matnr = ls_excel-value .
          gs_data-matnr = |{ gs_data-matnr ALPHA = IN WIDTH = 18 }| .
        WHEN 6. " 新序列号
          gs_data-sernr = ls_excel-value .
          TRANSLATE gs_data-sernr TO UPPER  CASE .
        WHEN 7. " 工厂代码
          gs_data-werks = ls_excel-value .
        WHEN 8. " 新WBS号
          gs_data-posid = ls_excel-value .
        WHEN 9. " 开始日期
          gs_data-begda = ls_excel-value .
        WHEN 10. " 结束日期
          gs_data-endda = ls_excel-value .
        WHEN 11. " 维修原因
          gs_data-repair_reason = ls_excel-value .
        WHEN 12. " 产品系列
          gs_data-zzmodelcode = ls_excel-value .
        WHEN 13. " 产品代码
          gs_data-zzmodelnumber = ls_excel-value .
        WHEN 14. " 产线
          gs_data-zzprodline = ls_excel-value .
      ENDCASE.

      AT END OF row .
        APPEND gs_data TO gt_data .
        CLEAR: gs_data .
      ENDAT .

    ENDLOOP.



  ENDMETHOD.

  METHOD get_filename .

    DATA:lt_file_name TYPE filetable,
         lv_file_name TYPE file_table,
         lv_rc        TYPE i.
    DATA: lv_title TYPE string .

    lv_title = TEXT-001.
    CALL METHOD cl_gui_frontend_services=>file_open_dialog
      EXPORTING
        window_title            = lv_title
        default_filename        = '*.xls'
        file_filter             = 'All Files (*.*)|*.*|Excel Files (*.xls)|*.xls|Word files(*.doc)|*.doc'
        initial_directory       = 'C:'           "初始化的目录
      CHANGING
        file_table              = lt_file_name
        rc                      = lv_rc
      EXCEPTIONS
        file_open_dialog_failed = 1
        cntl_error              = 2
        error_no_gui            = 3
        not_supported_by_gui    = 4
        OTHERS                  = 5.
    IF sy-subrc <> 0.
      MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
                 WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
    ENDIF.

    READ TABLE lt_file_name INDEX 1 INTO lv_file_name.
    MOVE lv_file_name TO p_file.
  ENDMETHOD.

  METHOD download .

    DATA: ls_name TYPE wwwdatatab,
          lt_mime TYPE TABLE OF w3mime.

    DATA lv_title TYPE string.
    lv_title = '选择路径'.

    DATA: lv_path      TYPE string,
          lv_fullpath  TYPE string,
          lv_extension TYPE string,
          lv_filename  TYPE string.
    CLEAR:ls_name,lt_mime,lv_extension,lv_filename.

    ls_name-relid = 'MI'.
    ls_name-objid = 'ZPPR128'.
    lv_extension  = 'xls'.
    lv_filename   = '(ZPP138)技改工单导入模板.xls'.

    CALL FUNCTION 'WWWDATA_IMPORT'
      EXPORTING
        key  = ls_name
      TABLES
        mime = lt_mime.
    CALL METHOD cl_gui_frontend_services=>file_save_dialog
      EXPORTING
        window_title         = lv_title "弹出窗口的标题
        default_extension    = lv_extension "默认的扩展名
        default_file_name    = lv_filename "默认的文件名
      CHANGING
        filename             = lv_filename "改后需要保存的文件名
        path                 = lv_path    "文件路径
        fullpath             = lv_fullpath "文件名+路径
      EXCEPTIONS
        cntl_error           = 1
        error_no_gui         = 2
        not_supported_by_gui = 3
        OTHERS               = 4.
    IF lv_fullpath IS INITIAL.
      MESSAGE s000(oo) WITH '操作已由用户取消'.
    ELSE.
      CALL FUNCTION 'GUI_DOWNLOAD'
        EXPORTING
          filename = lv_fullpath "文件名+路径
          filetype = 'BIN'
        TABLES
          data_tab = lt_mime. "存储模板的表
      p_file = lv_fullpath.
    ENDIF.

  ENDMETHOD.



ENDCLASS .


CLASS zcl_alv IMPLEMENTATION.

  METHOD constructor .
    mo_data = io_data .
  ENDMETHOD.

  METHOD add_field.

    CLEAR ls_fieldcat.
    ls_fieldcat-fieldname    = pv_field.
    ls_fieldcat-scrtext_l    = pv_text.
    ls_fieldcat-outputlen    = pv_len.
    ls_fieldcat-checkbox    = pv_check.
    ls_fieldcat-edit        = pv_edit.

    CASE ls_fieldcat-fieldname.
      WHEN 'MATNR'.
        ls_fieldcat-ref_table  = 'MARA' .
        ls_fieldcat-ref_field  = 'MATNR' .
      WHEN 'MATNR_ORGIN'.
        ls_fieldcat-ref_table  = 'MARA' .
        ls_fieldcat-ref_field  = 'MATNR' .
      WHEN OTHERS.
    ENDCASE.

    APPEND ls_fieldcat TO lt_fieldcat.

  ENDMETHOD .

  METHOD field_catalog.

    CLEAR: ls_layout_s.
    ls_layout_s-sel_mode = 'A'.
    ls_layout_s-cwidth_opt = 'X'.

    CLEAR: lt_fieldcat .

    add_field( pv_field = 'SEL' pv_text = '复选框' pv_len = '1' pv_check = 'X' pv_edit = 'X' ).
    add_field( pv_field = 'STATUS' pv_text = '状态' pv_len = '4'  pv_check = '' pv_edit = ''  ).
    add_field( pv_field = 'MESSAGE' pv_text = '消息' pv_len = '30'  pv_check = '' pv_edit = ''  ).
    add_field( pv_field = 'NUM' pv_text = '序号' pv_len = '4'   pv_check = '' pv_edit = ''  ).
    add_field( pv_field = 'MATNR_ORGIN' pv_text = '原物料编码' pv_len = '10'   pv_check = '' pv_edit = ''  ).
    add_field( pv_field = 'SERNR_ORGIN' pv_text = '原序列号' pv_len = '3'   pv_check = '' pv_edit = ''  ).
    add_field( pv_field = 'CHARG_ORGIN' pv_text = '原批次号' pv_len = '24'   pv_check = '' pv_edit = ''  ).
    add_field( pv_field = 'MATNR' pv_text = '新物料编码' pv_len = '50'   pv_check = '' pv_edit = ''  ).
    add_field( pv_field = 'SERNR' pv_text = '新序列号' pv_len = '50'   pv_check = '' pv_edit = ''  ).
    add_field( pv_field = 'WERKS' pv_text = '工厂代码' pv_len = '50'   pv_check = '' pv_edit = ''  ).
    add_field( pv_field = 'POSID' pv_text = '新WBS号' pv_len = '24'   pv_check = '' pv_edit = ''  ).
    add_field( pv_field = 'BEGDA' pv_text = '开始日期' pv_len = '8'   pv_check = '' pv_edit = ''  ).
    add_field( pv_field = 'ENDDA' pv_text = '结束日期' pv_len = '8'   pv_check = '' pv_edit = ''  ).
    add_field( pv_field = 'REPAIR_REASON' pv_text = '维修原因' pv_len = '50'   pv_check = '' pv_edit = ''  ).
    add_field( pv_field = 'ZZMODELCODE' pv_text = '产品系列' pv_len = '50'   pv_check = '' pv_edit = ''  ).
    add_field( pv_field = 'ZZMODELNUMBER' pv_text = '产品代码' pv_len = '50'   pv_check = '' pv_edit = ''  ).
    add_field( pv_field = 'ZZPRODLINE' pv_text = '产线' pv_len = '50'   pv_check = '' pv_edit = ''  ).


  ENDMETHOD.

  METHOD exclude_fun.

    CLEAR: lt_exclude .
    DATA: lv_exclude TYPE ui_func .
    lv_exclude = cl_gui_alv_grid=>mc_fc_loc_cut.
    APPEND lv_exclude TO lt_exclude.
    lv_exclude = cl_gui_alv_grid=>mc_fc_loc_delete_row.
    APPEND lv_exclude TO lt_exclude.
    lv_exclude = cl_gui_alv_grid=>mc_fc_loc_insert_row.
    APPEND lv_exclude TO lt_exclude.
    lv_exclude = cl_gui_alv_grid=>mc_fc_loc_paste.
    APPEND lv_exclude TO lt_exclude.
    lv_exclude = cl_gui_alv_grid=>mc_fc_loc_paste_new_row.
    APPEND lv_exclude TO lt_exclude.
    lv_exclude = cl_gui_alv_grid=>mc_fc_print.
    APPEND lv_exclude TO lt_exclude.
    lv_exclude = cl_gui_alv_grid=>mc_fc_info.
    APPEND lv_exclude TO lt_exclude.
    lv_exclude = cl_gui_alv_grid=>mc_fc_loc_copy.
    APPEND lv_exclude TO lt_exclude.
    lv_exclude = cl_gui_alv_grid=>mc_fc_loc_append_row.
    APPEND lv_exclude TO lt_exclude.
    lv_exclude = cl_gui_alv_grid=>mc_fc_loc_copy_row.
    APPEND lv_exclude TO lt_exclude.
    lv_exclude = cl_gui_alv_grid=>mc_fc_loc_undo.
    APPEND lv_exclude TO lt_exclude.

  ENDMETHOD.

  METHOD display .

    field_catalog( ) .
    exclude_fun( ) .

    IF lr_grid IS NOT INITIAL.

      lr_grid->refresh_table_display( ) .

    ELSE.

      register_event( ) .

      CALL METHOD lr_grid->set_table_for_first_display
        EXPORTING
*         is_variant           = gs_variant
          i_save               = 'A'
          is_layout            = ls_layout_s
          it_toolbar_excluding = lt_exclude
        CHANGING
          it_outtab            = gt_data
          it_fieldcatalog      = lt_fieldcat.

      IF sy-subrc <> 0.
        MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
        WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
      ENDIF.

    ENDIF.

    CALL SCREEN '0900'.

  ENDMETHOD .


  METHOD handle_toolbar.

    DATA: ls_toolbar TYPE stb_button .

    CLEAR: ls_toolbar.
    MOVE   'ALL'     TO   ls_toolbar-function.
    MOVE   '全选'   TO   ls_toolbar-text.
    MOVE    '@4B@'   TO   ls_toolbar-icon .
    APPEND ls_toolbar TO e_object->mt_toolbar .

    CLEAR: ls_toolbar.
    MOVE   'SAL'     TO   ls_toolbar-function.
    MOVE   '全不选'   TO   ls_toolbar-text.
    MOVE    '@4D@'   TO   ls_toolbar-icon .
    APPEND ls_toolbar TO e_object->mt_toolbar .

    CLEAR: ls_toolbar.
    MOVE   'EXECUTE'  TO   ls_toolbar-function.
    MOVE   '创建工单'   TO   ls_toolbar-text.
    MOVE    '@4A@'   TO   ls_toolbar-icon .
    APPEND ls_toolbar TO e_object->mt_toolbar .


  ENDMETHOD.                    "handle_onf4

  METHOD handle_user_command.

    DATA: lv_type    TYPE char1,
          lv_message TYPE char100.

    CASE e_ucomm.
      WHEN 'EXECUTE'.

        mo_data->run( ) .

      WHEN 'ALL'.

        select_all_items( ) .

      WHEN 'SAL'.

        not_select_all_items( ) .

      WHEN OTHERS.
    ENDCASE.

    CALL METHOD lr_grid->refresh_table_display
      EXPORTING
        i_soft_refresh = 'X'
      EXCEPTIONS
        finished       = 1
        OTHERS         = 2.



  ENDMETHOD.

  METHOD handle_double_click.

    READ TABLE gt_data INTO DATA(ls_line) INDEX es_row_no-row_id .
    IF sy-subrc =  '0' AND ls_line-status = gc_green_light.
      SET PARAMETER ID 'ANR' FIELD ls_line-order.
      CALL TRANSACTION 'CO02' AND SKIP FIRST SCREEN.
    ENDIF.

  ENDMETHOD.

  METHOD handle_data_changed .
*    PERFORM frm_data_changed USING er_data_changed .
  ENDMETHOD .


  METHOD register_event .

    IF lr_grid IS INITIAL .
      CREATE OBJECT lr_grid
        EXPORTING
          i_parent = cl_gui_container=>screen0.
    ENDIF.

    SET HANDLER me->handle_toolbar FOR lr_grid .
    SET HANDLER me->handle_user_command FOR lr_grid .
    SET HANDLER me->handle_double_click FOR lr_grid.
    SET HANDLER me->handle_data_changed FOR lr_grid .

    CALL METHOD lr_grid->register_edit_event
      EXPORTING
        i_event_id = cl_gui_alv_grid=>mc_evt_modified
      EXCEPTIONS
        error      = 1
        OTHERS     = 2.

  ENDMETHOD .


  METHOD select_all_items .

*    DATA: lt_rows   TYPE  lvc_t_row,
*          lt_row_no TYPE lvc_t_roid.
*
*    lr_grid->get_selected_rows( IMPORTING et_index_rows = lt_rows
*                                              et_row_no =  lt_row_no ) .
*
*    LOOP AT lt_rows INTO DATA(ls_row).
*      READ TABLE gt_data ASSIGNING FIELD-SYMBOL(<fs_data>) INDEX ls_row-index.
*      IF sy-subrc =  '0'.
*        <fs_data>-sel = 'X' .
*      ENDIF.
*    ENDLOOP.

    LOOP AT gt_data ASSIGNING FIELD-SYMBOL(<fs_data>).
      <fs_data>-sel = 'X' .
    ENDLOOP.


  ENDMETHOD .
  METHOD  not_select_all_items .

    LOOP AT gt_data ASSIGNING FIELD-SYMBOL(<fs_data>).
      <fs_data>-sel = '' .
    ENDLOOP.
  ENDMETHOD .



ENDCLASS .